<?php
/**
 * Created by 上海互教教育科技有限公司.
 * User: 刘红伟
 * QQ  : 454303753
 * Date: 2017/5/31 0031
 * Time: 下午 15:15
 */
namespace  App\Http\Controllers\v1;
use App\Orders;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class OrdersController extends BaseController
{
    public function add(Request $request)
    {

        if($request)
        {
            $order_obj  =  new Orders();
            $order_obj->order_id   =   $request->order_id;
            $order_obj->member_id      =   $request->member_id;
            $order_obj->order_name      =   $request->order_name;
            $order_obj->price      =   $request->price;
            $order_obj->order_code      =   $request->order_code;
            $order_obj->order_status      =   $request->order_status;
            $order_obj->itime_id      =   $request->itime_id;
            $order_obj->hairdresser_id      =   $request->hairdresser_id;
            $order_obj->subscribe_time      =   time();
            $order_obj->remarks      =   $request->remarks;
            $order_obj->create_at      =   $request->create_at; 

            if($order_obj->save())
            {
                return response()->json(array('status'=>1),200);
            };

            return response()->json(array('status'=>0),200);

        }

    }

    public function get_data($order_id)
    {
        $orders =   Orders::find($order_id);
        if($orders)
        {
            return response()->json($orders,200);
        }else
        {
            return response()->json((object)array(),200);
        }

    }
    public function get_all()
    {
        $orders =   Orders::all();

        if($orders)
        {
            return response()->json($orders,200);
        }else
        {
            return response()->json((object)array(),200);
        }

    }
    public function get_hairdresser_order()
    {
        date_default_timezone_set('Asia/Shanghai');
        $hairdresser_id = 1;
        if($hairdresser_id)
        {
            /**
              * 一天开始时间1497661200 2017/6/17 09:00:00   2017/6/17 00:00:00  1497628800
              * 一天结束时间1497709800 2017/6/17 22:30:00   2017/6/17 00:00:00  1497628800
              * 当前时间：1497688655  2017/6/17 16:37:35
              * 1800/miao
             *
             * 1497688655 - 1497661200 = x
             * x/1800 = y
             *
             * z= 1800
             * n = 28
             *
             * a=['09:30']
             * ....
             * a=['22:30']
             *
             *
             * [1497661200]       =
             * [1497661200+(n*z)] =
             * [1497709800]       =
             *
             * $
             *
             *
             *
             *
             */
            $use_time  =   [];
            $n          =   28;

            $orders =   Orders::all()->where('hairdresser_id',$hairdresser_id)->where('subscribe_time','>',strtotime(date('Y-m-d',time())));

            if($orders)
            {
                $time    = time();
                if($time > (strtotime(date('Y-m-d',time()))+9*3600))
                {
                    $use_time[(strtotime(date('Y-m-d',time()))+9*3600)][0]   =   1;
                    for($i=1;$i<=$n;$i++)
                    {

                        $use_time[(strtotime(date('Y-m-d',time()))+9*3600 + ($i*1800))][$i]   =   1;
                    }
                }



                foreach($orders as $v)
                {

                    for($i=1;$i<=$n;$i++)
                    {

                        if(strtotime(date('Y-m-d',time()))+9*3600  ==  $v->subscribe_time)
                        {
                            $use_time[(strtotime(date('Y-m-d',time()))+9*3600)][0]   =   0;
                        }else
                        {

                            if((strtotime(date('Y-m-d',time()))+9*3600 + ($i*1800)) == $v->subscribe_time)
                            {
                                $use_time[(strtotime(date('Y-m-d',time()))+9*3600 + ($i*1800))][$i]   =   0;
                            }


                        }

                    }




                }

                if($time < (strtotime(date('Y-m-d',time()))+9*3600))
                {
                    $use_time[(strtotime(date('Y-m-d',time()))+9*3600)][0]   =   0;
                    for($i=1;$i<=$n;$i++)
                    {

                        $use_time[(strtotime(date('Y-m-d',time()))+9*3600 + ($i*1800))][$i]   =   0;
                    }
                }

                if($time > strtotime(date('Y-m-d',time()))+22*3600+1800)
                {
                    $use_time[(strtotime(date('Y-m-d',time()))+9*3600)][0]   =   0;
                    for($i=1;$i<=$n;$i++)
                    {

                        $use_time[(strtotime(date('Y-m-d',time()))+9*3600 + ($i*1800))][$i]   =   0;
                    }
                }

                //当前时间
                return response()->json($use_time,200);
            }
            return response()->json((object)array(),200);
        }
    }

	public function test()
	{
		exit('ss111');
	}
	public function orderList( $type = 0 ){
		//0,1,2未处理，已结算，退订
		//$orderList = DB::table('orders')->lists('order_id', 'order_name','price','order_status','create_at');
		
		//echo json_encode( $orderList , true );

		$orderList = array(
			array(
				'order_id' => 1, 
				'order_name' => 'mc1',
				'price' => 15, 
				'order_status' => 0,
				'create_at' => '2017-06-22',
			),	
			array(
				'order_id' => 1, 
				'order_name' => 'mc2',
				'price' => 25, 
				'order_status' => 0,
				'create_at' => '2017-06-23',
			),	
			array(
				'order_id' => 1, 
				'order_name' => 'mc3',
				'price' => 155, 
				'order_status' => 0,
				'create_at' => '2017-06-24',
			),	
		
		
		
		);
		
		echo json_encode( $orderList , true );
	
	}


}